package com.xltq.controller;

import com.xltq.dao.IAddressDao;
import com.xltq.entity.Address;
import com.xltq.entity.User;
import com.xltq.service.IAddressService;
import com.xltq.service.impl.IAddressServiceImpl;
import com.xltq.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@WebServlet("/address")
public class AddressController extends BaseServlet {
    //创建地址service层对象提供业务操作
    private IAddressService addressService = new IAddressServiceImpl();
    //1.展示用户所有地址信息
    public String show (HttpServletRequest request, HttpServletResponse response) throws Exception {
            //1.判断用户是否登录
        HttpSession session = request.getSession();
        User loginUser = (User)session.getAttribute("loginUser");
        if(Objects.isNull(loginUser)){
            //证明没有登录
            session.setAttribute("msg", "请先登录在查询地址!");
            return Constants.REDIRECT+"/login.jsp";
        }
        //2.获取uid
        int uid = loginUser.getUid();
        List<Address> addressByUid = addressService.findAddressByUid(uid);
        //查询到数据存储在作用域中,以便在页面中展示数据
        request.setAttribute("list", addressByUid);
        return  Constants.FORWARD+"/self_info.jsp";
    }

    //2.添加用户地址信息
    public String add (HttpServletRequest request, HttpServletResponse response) throws Exception {
       //1.获取页面中提供的用户地址信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address, parameterMap);
        addressService.saveAddress(address);
        //2.跳转到展示页面(是servlet)
        return  Constants.FORWARD+"/address?method=show";
    }

    //3.删除用户地址信息
    public String delete (HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取页面传递的aid数据
        String aid = request.getParameter("aid");
        addressService.deleteAddressByAid(aid);
        //2.跳转到展示页面(是servlet)
        return  Constants.FORWARD+"/address?method=show";
    }
    //4.更新用户地址信息(全更新)
    public String update (HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取页面中提供的用户地址信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address, parameterMap);
        addressService.setAddress(address);
        //2.跳转到展示页面(是servlet)
        return  Constants.FORWARD+"/address?method=show";
    }
    //5.更新用户地址状态
    public String setDefault (HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取存储在请求参数中aid
        String aid = request.getParameter("aid");
        //2.获取当前登录用户信息的中uid
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        //3.设置默认地址
        addressService.setaddressToDefault(aid, loginUser.getUid());
        //4.跳转到展示页面(是servlet)
        return  Constants.FORWARD+"/address?method=show";
    }
}
